package logic.files;

import database.IDBObject;
import database.Record;
import logic.baselogic.Database;

import java.io.BufferedWriter;
import java.io.FileWriter;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Collections;

/**
 * Created by IntelliJ IDEA.
 * User: grigory.rozhkov
 * Date: 12.09.12
 * Time: 23:37
 * To change this template use File | Settings | File Templates.
 */
public class SimpleFileDatabaseSaver implements IDatabaseSaver {

    //method merges two lists in alph order
    private ArrayList<IDBObject> prepareList(Database database) {
        ArrayList<IDBObject> addedList = database.getAddedList();
        Collections.sort(addedList);
        ArrayList<IDBObject> baseList = database.getBaseList();
        int baseListCursor = 0;
        int addedArrayCursor = 0;
        int baseListLength = baseList.size();
        int addedArrayLength = addedList.size();

        ArrayList<IDBObject> finalList = new ArrayList<IDBObject>(baseListLength + addedArrayCursor + Database.MAX_NON_SAVED_TRANSACTIONS);
        while ((baseListCursor != baseListLength) || (addedArrayCursor != addedArrayLength)) {
            if (baseListCursor == baseListLength) {
                while (addedArrayCursor != addedArrayLength) {
                    if (!addedList.get(addedArrayCursor).isDeleted()) finalList.add(addedList.get(addedArrayCursor));
                    addedArrayCursor++;
                }
            }

            if (addedArrayCursor == addedArrayLength) {
                while (baseListCursor != baseListLength) {
                    if (!baseList.get(baseListCursor).isDeleted()) finalList.add(baseList.get(baseListCursor));
                    baseListCursor++;
                }
            }
            if (baseListCursor != baseListLength) while (baseList.get(baseListCursor).isDeleted()) baseListCursor++;
            if (addedArrayCursor != addedArrayLength) while (addedList.get(addedArrayCursor).isDeleted()) addedArrayCursor++;

            if ((baseListCursor != baseListLength) && (addedArrayCursor != addedArrayLength)) {
                if (baseList.get(baseListCursor).compareTo(addedList.get(addedArrayCursor)) > 0) {
                    while ((addedArrayCursor < addedArrayLength)
                            && (baseList.get(baseListCursor).compareTo(addedList.get(addedArrayCursor)) > 0)) {
                        if (!addedList.get(addedArrayCursor).isDeleted()) finalList.add(addedList.get(addedArrayCursor));
                        addedArrayCursor++;
                    }
                } else {
                    while ((baseListCursor < baseListLength)
                            && (baseList.get(baseListCursor).compareTo(addedList.get(addedArrayCursor)) < 0)) {
                        if (!baseList.get(baseListCursor).isDeleted()) finalList.add(baseList.get(baseListCursor));
                        baseListCursor++;
                    }
                }
            }
        }
        return finalList;
    }

    public void saveBase(Database database) {
        BufferedWriter bufferedWriter = null;
        try {
            bufferedWriter = new BufferedWriter(new FileWriter("src/database/save/db", false));
        } catch (IOException e) {
            e.printStackTrace();
        }
        if (bufferedWriter != null) {
            for (IDBObject currentObject : prepareList(database)) {
                Record record = (Record) currentObject;
                try {
                    bufferedWriter.write(record.getId());
                    bufferedWriter.newLine();
                    bufferedWriter.write(record.getDescription());
                    bufferedWriter.newLine();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            try {
                bufferedWriter.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
}
